<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	
	
	
	
	/**
	 * 
	 * sessions
	 * 
	 * controller to manage all the session
	 */
	class sessions extends CI_Controller {
		
		
		
		
		
		/**
		 * __construct
		 */
		public function __construct () {
			parent::__construct();
		}
		// end: __construct
		
		
		
		
		
		/**
		 * 
		 * index
		 */
		public function index () {
			
		}
		// end: index
		
		
		
		
		
		/**
		 * 
		 * login
		 * 
		 * method to show the login form, this form only show the 
		 * form if the session has no started
		 */
		public function login () {
			
			// if no is logged in
			if ( !$this->common->is_logged_in() ) {
				
				// load the template top
				$this->load->view('templates/template-web-top');
				
				/**
				 * @var Array $login_data (this carry the information about the variables in the view)
				 */
				$login_data = array(
					'form_url'		=> 'sessions/login/' . (($this->input->get('redirect_to'))?'?redirect_to=' . urlencode($this->input->get('redirect_to')) . '':'') . '',
					'msg_type'		=> '',
					'msg_content'	=> ''
				);
				// end: $login_data
				
				// load form_falidation library
				$this->load->library('form_validation');
				
				// beg: setting rules for validation
				$this->form_validation->set_rules('cisco_loginform_username', '<b><i>username</i></b>', 'username|required');
				$this->form_validation->set_rules('cisco_loginform_password', '<b><i>password</i></b>', 'required');
				// end: setting rules for validation
				
				// if form isn't run
				if ( $this->form_validation->run() == TRUE ) {
					
					// beg: intializing ldap configuration
					$ldap_config = array(
						'ldap_server_name'		=> '127.0.0.1',
						'ldap_server_domain'	=> '127.0.0.1',
						'ldap_domain_name'		=> 'dc=example,dc=company,dc=com',
						'ldap_username'			=> $this->input->post('cisco_loginform_username'),
						'ldap_password'			=> $this->input->post('cisco_loginform_password')
					);
					$this->ldap->initialize($ldap_config);
					// end: intializing ldap configuration
					
					// checking if the connection is successful
					if ( $this->ldap->set_connection_true() == 200 ) {
						
						// loading the session model
						$this->load->model('sessions_mdl', 'sessions');
						
						// setting session
						$this->sessions->set_session($this->input->post('cisco_loginform_username'));
						
						// redirecting
						redirect((($this->input->get('redirect_to'))? urldecode($this->input->get('redirect_to')):'/'), 'location');
						
						$login_data['msg_type']		= 'success';
						$login_data['msg_content']	= 'the username or password you entered is incorrect.';
					}
					
					// if the connection is unsuccesful
					else {
						$login_data['msg_type']		= 'error';
						$login_data['msg_content']	= 'the username or password you entered is incorrect.';
					}
				}
					$this->load->view('sessions/sessions-form-login', $login_data);
				
				// load the template bottom 
				$this->load->view('templates/template-web-bottom');
			}
			
			// if is logged in
			else
				redirect((($this->input->get('redirect_to'))? urldecode($this->input->get('redirect_to')):'/'), 'location');
		}
		// end: login
		
		
		
		
		
		/**
		 * 
		 * logout
		 * 
		 * method to end a session
		 */
		public function logout () {
			// loading the session model
			$this->load->model('sessions_mdl', 'sessions');
			
			$this->sessions->unset_session();
			
			// redirecting the 
			redirect((($this->input->get('redirect_to'))? urldecode($this->input->get('redirect_to')):'/'), 'location');
		}
		// end: logout
	}
	// end: sessions	